package com.aircity.commonuse.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * desc : Redis 工具类
 * author : gongfangchao
 * date : 2020/6/11
 * Copyright (c) 2015 jigoon
 **/
@Component
public class RedisUtil {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * Redis集合-添加元素
     * @param key 键
     * @param values 值
     * @return 操作成功个数
     */
    public long sAdd(String key, Object ...values) {
        try {
            return this.redisTemplate.opsForSet().add(key, values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * Redis-集合-获取全部元素
     * @param key 键
     * @return 全部元素
     */
    public Set<Object> sGet(String key) {
        return this.redisTemplate.opsForSet().members(key);
    }

    /**
     * Redis-集合-选出count个元素，元素从集合中删除
     * @param key 键
     * @return 随机弹出的元素
     */
    public Object sPop(String key) {
        return this.redisTemplate.opsForSet().pop(key);
    }

}
